const db = require("../db")
const { querySql , queryOne }=require("../db")
const { UPLOAD_PATH } = require('../utils/constant')
var fs = require('fs');

function exists(data){
    return false
}

 function removeBanner(data){
    return  new Promise( async (resolve,reject)=>{
        try{
            if(data.id){
                const removeBookSql = `delete from fs_bannerlist where id='${data.id}'`
                await db.querySql(removeBookSql)
                var filename = `${UPLOAD_PATH}/Img/${data.filename}`;
                fs.unlink(filename, function(err) {
                if (err) {
                    reject(new Error('删除失败'))
                    console.log('删除失败',err);
                }else{
                    resolve()
                    console.log("删除成功");
                }
            });
        }
        }catch(e){
            reject()
        }
    
   })
   
}


function removeCentre(data){//删除活动
    return  new Promise( async (resolve,reject)=>{
        try{
            if(data.id){
                const removeBookSql = `delete from fs_centrelist where id='${data.id}'`
                await db.querySql(removeBookSql)
                var filename = `${UPLOAD_PATH}/Img/${data.filename}`;
                fs.unlink(filename, function(err) {
                if (err) {
                    reject(new Error('删除失败'))
                    console.log('删除失败',err);
                }else{
                    resolve()
                    console.log("删除成功");
                }
            });
        }
        }catch(e){
            reject()
        }
    
   })
   
}

function removeCategory(data){//删除分类
    return  new Promise( async (resolve,reject)=>{
        try{
            if(data.id){
                const removeBookSql = `delete from fs_category where id='${data.id}'`
                await db.querySql(removeBookSql)
                var filename = `${UPLOAD_PATH}/Img/${data.filename}`;
                fs.unlink(filename, function(err) {
                if (err) {
                    reject(new Error('删除失败'))
                    console.log('删除失败',err);
                }else{
                    resolve()
                    console.log("删除成功");
                }
            });
        }
        }catch(e){
            reject()
        }
    
   })
   
}





function BannerList(){//查询所有轮播图
    return querySql(`select * from fs_bannerlist`)
}


function getBanner(data){//查询轮播图的详情
    return queryOne(`select * from fs_bannerlist where id='${data.id}'`)
}

function getCreate(data){//查询活动的详情
    return queryOne(`select * from fs_centrelist where id='${data.id}'`)
}


function getCategory(data){//查询分类的详情
    return queryOne(`select * from fs_category where id='${data.id}'`)
}


function updataBanner(data){
    return  new Promise( async (resolve,reject)=>{
        try{
            await db.update(data,'fs_bannerlist',`where id='${data.id}'`)
           
            resolve()
            
         }catch(e){
             reject()
         }
    
   })
}

function updataCentre(data){//更新活动
    return  new Promise( async (resolve,reject)=>{
        try{
            await db.update(data,'fs_centrelist',`where id='${data.id}'`)
           
            resolve()
            
         }catch(e){
             reject()
         }
    
   })
}


function insertBanner(data){//添加轮播图
   return  new Promise( async (resolve,reject)=>{
        try{
           const result =  exists(data)
           if(result){
           }else{
                await db.insert(data,'fs_bannerlist')
                resolve()
           }
        }catch(e){
            reject()
        }
    
   })

}


function insertCentre(data){//添加活动
    return  new Promise( async (resolve,reject)=>{
         try{
            const result =  exists(data)
            if(result){
            }else{
                 await db.insert(data,'fs_centrelist')
                 resolve()
            }
         }catch(e){
             reject()
         }
     
    })
 }

 function insertCategory(data){//添加分类
    return  new Promise( async (resolve,reject)=>{
         try{
            const result =  exists(data)
            if(result){
            }else{
                 await db.insert(data,'fs_category')
                 resolve()
            }
         }catch(e){
             reject()
         }
     
    })
 }

 

function centreList(){//查询所有活动
    return querySql(`select * from fs_centrelist`)
}


function getCategorylist(){//查询所有分类
    return querySql(`select count(a.category_id) as count, c.* from fs_category c left outer join fs_article a on c.id = a.category_id group by c.id `)
}

function updataCategory(data){//更新分类
    return  new Promise( async (resolve,reject)=>{
        try{
            await db.update(data,'fs_category',`where id='${data.id}'`)
            resolve()
         }catch(e){
             reject()
         }
    
   })
}






module.exports = {
    //轮播
    insertBanner,
    BannerList,
    getBanner,
    removeBanner,
    updataBanner,
    //活动
    insertCentre,
    centreList,
    removeCentre,
    getCreate,
    updataCentre,
    //分类
    getCategorylist,
    getCategory,
    updataCategory,
    insertCategory,
    removeCategory
}